//
//  ReplyCommentViewController.m
//  CYWFrame
//
//  Created by Macx on 2017/8/9.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "ReplyCommentViewController.h"
#import "AgreeCommentTableViewCell.h"
#import "HomeServer.h"
#import "CommentViewController.h"
#import "QAZoneTableViewCell.h"
#import "UILabel+copy.h"

#import "AliPayServer.h"

#import "UIViewController+KNSemiModal.h"

@interface ReplyCommentViewController () <UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, strong) NSMutableArray *commentArr;
@property (nonatomic, assign) NSInteger page; // 网络请求页数

@property (nonatomic, strong) Question *questionModel;

@property (nonatomic, assign) NSInteger reward; // 特殊页面 获取酬劳
@property (nonatomic, assign) NSInteger qaUserId; // 这个问题的用户的id
@property (nonatomic, assign) NSInteger questionIsClosed; // 这个问题打赏是否关闭

@property (nonatomic, strong) UIButton *cainaBtn;

@end

@implementation ReplyCommentViewController {
    UITableView *_tableView;
    AgreeCommentTableViewCell *_cell;
    QAZoneTableViewCell       *_qaCell;
    
    UIView  *_bottomView;
    UITextField *_textField;
}


- (void)viewWillAppear:(BOOL)animated {
    
    [self GETComments];
    
}
//  主页tableView是否滚动接收到通知
- (void)notificationPay:(NSNotification *)notification {
    NSLog(@"－－－－－答案详情收到微信支付接收到通知------");
    [self.cainaBtn setTitle:@"已采纳" forState:UIControlStateNormal];
    [self.cainaBtn setBackgroundColor:kLightGrayColor];
    self.cainaBtn.userInteractionEnabled = NO;
}

- (void)postComment:(NSNotification *)notification {
    NSLog(@"======评论成功======");
    [self GETComments];
}

//移除广播监听。
-(void)dealloc{
    [[NSNotificationCenter defaultCenter]removeObserver:self];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    if (self.type == 3) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(notificationPay:) name:@"payReply" object:nil];
    }
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(postComment:) name:@"postComment" object:nil];
    
    self.page = 1;
    
    [self buildUI];
    
    [self refreshData];
    
}

#pragma mark ==========构建UI==========
- (void)buildUI {
    
    self.view.backgroundColor = UISeparatedColorRGB;
    
    self.navigationItem.title = @"评论回复";
    
    if (self.type == 3) {
        self.navigationItem.title = @"答案详情";
        [self GETAnswerDetail];
    }
    
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-49-64) style:UITableViewStylePlain];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc]init];
    _tableView.backgroundColor = kClearColor;
    
    if (self.type == 3) {
        [_tableView registerNib:[UINib nibWithNibName:@"QAZoneTableViewCell" bundle:nil] forCellReuseIdentifier:@"qaCell"];
    }
    else
        [_tableView registerNib:[UINib nibWithNibName:@"AgreeCommentTableViewCell" bundle:nil] forCellReuseIdentifier:@"comment"];
    
    
    // 底部
    UIImageView *imgView = [[UIImageView alloc] initWithFrame:CGRectMake(0, SCREEN_HEIGHT-49-64, SCREEN_WIDTH, 49)];
    [self.view addSubview:imgView];
    imgView.image = IMAGE(@"reply_bg");
    [imgView addTarget:self action:@selector(postCommentClick:)];
    
    if (self.type == 2) {
        _tableView.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-64);
        imgView.hidden = YES;
    }
}

#pragma mark ==========更新数据==========
- (void)refreshData {
    __weak typeof (self) weakSelf = self;
    
    _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        NSLog(@"下拉刷新");
        weakSelf.page = 1;
        [weakSelf GETComments];
    }];
    
    _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        NSLog(@"上拉加载");
        weakSelf.page++;
        [weakSelf GETComments];
    }];
    
    
}

#pragma mark ==========UITableView==========
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    if (self.type == 3) {
        
        _qaCell = [tableView dequeueReusableCellWithIdentifier:@"qaCell"];
        _qaCell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        Question *model = self.commentArr[indexPath.row];
        _qaCell.model = model;
        
        _qaCell.title.isCopyable = YES;
        
        if (indexPath.row == 0) {
            
//            _qaCell.qaBtn.hidden = YES;
            _qaCell.line.hidden = YES;
            _qaCell.praise.hidden = NO;
            _qaCell.disPraise.hidden = NO;
            
            // 赞
            if (model.is_like == 1) {
                [_qaCell.praise setImage:[IMAGE(@"index_zan_ed") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            }
            else
                [_qaCell.praise setImage:[IMAGE(@"index_zan") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [_qaCell.praise setTitle:[NSString stringWithFormat:@" %ld", model.likes_count] forState:UIControlStateNormal];
            // 踩
            if (model.is_unlike == 1) {
                [_qaCell.disPraise setImage:[IMAGE(@"index_yicai") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            }
            else
                [_qaCell.disPraise setImage:[IMAGE(@"question_cai") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
            [_qaCell.disPraise setTitle:[NSString stringWithFormat:@" %ld", model.unlikes_count] forState:UIControlStateNormal];
            
            CGSize size1 = [LZCTools textSizeWithText:_qaCell.viewNum.text font:_qaCell.viewNum.font];
            CGSize size2 = [LZCTools textSizeWithText:_qaCell.commentNum.text font:_qaCell.commentNum.font];
            CGSize size3 = [LZCTools textSizeWithText:_qaCell.praise.titleLabel.text font:_qaCell.praise.titleLabel.font];
            CGSize size4 = [LZCTools textSizeWithText:_qaCell.disPraise.titleLabel.text font:_qaCell.disPraise.titleLabel.font];
            
            CGFloat Y = _qaCell.viewNum.y;
            
            _qaCell.viewNum.frame = CGRectMake(10, Y, size1.width, size1.height);
            _qaCell.praise.frame = CGRectMake(_qaCell.viewNum.x+_qaCell.viewNum.width+10, Y, size3.height+size3.width, size3.height);
            _qaCell.disPraise.frame = CGRectMake(_qaCell.praise.x+_qaCell.praise.width, Y, size4.width+size4.height, size4.height);
            _qaCell.commentNum.frame = CGRectMake(_qaCell.disPraise.x+_qaCell.disPraise.width+10, Y, size2.width, size2.height);
            
            
            NSInteger currUserId = [[GetUserDefaule(USERINFO) valueForKey:@"user_id"] integerValue];
            NSInteger answerUserId = [[self.questionModel.author valueForKey:@"user_id"] integerValue];
//            NSInteger anser_price = [[self.questionModel valueForKey:@"answer_price"] integerValue];
            
            // 如果 1、操作手机是这个问题的题主 2、这个答案的用户不是问题题主 3、这个问题的赏金大于0 4、这个问题悬赏中 。符合四个状态才开启money
            if (self.qaUserId == currUserId && answerUserId != self.qaUserId && self.reward > 0 && self.questionIsClosed == 0) {
                
                [_qaCell.money setTitle:[NSString stringWithFormat:@"采纳%.2f", [[NSString stringWithFormat:@"%ld", self.reward] floatValue]/100] forState:UIControlStateNormal];
                CGSize size5 = [LZCTools textSizeWithText:_qaCell.money.titleLabel.text font:kUHSystemFontWithSize(13)];
                CGFloat moneyW = 10+size5.width;
                _qaCell.money.frame = CGRectMake(SCREEN_WIDTH-10-moneyW, 0, moneyW, size3.height+8);
                _qaCell.money.centerY = _qaCell.viewNum.centerY;
                
                _qaCell.money.userInteractionEnabled = YES;
                [_qaCell.money addTarget:self action:@selector(payQuestionClick:) forControlEvents:UIControlEventTouchUpInside];
                
            }
            else
                _qaCell.money.hidden = YES;
            
            
            int state = [LZCTools thinkEnv];
            if (state == 0) {
                _qaCell.money.hidden = YES;
            }
            
            [_qaCell.qaBtn setTitleColor:kWhiteColor forState:UIControlStateNormal];
            CGSize size6 = [LZCTools textSizeWithText:@"未关注" font:kUHSystemFontWithSize(13)];
            CGFloat focusWidth = 10+size6.width+size6.height;
            _qaCell.qaBtn.frame = CGRectMake(SCREEN_WIDTH-10-focusWidth, 0, focusWidth, size6.height+8);
            _qaCell.qaBtn.centerY = _qaCell.headPic.centerY;
            
            if ([[GetUserDefaule(USERINFO) valueForKey:@"user_id"] isEqualToString:[model.author valueForKey:@"user_id"]]) {
                _qaCell.qaBtn.hidden = YES;
            }
            else
                _qaCell.qaBtn.hidden = NO;
            
            if ([[model.author valueForKey:@"is_like"] intValue] == 1) {
                [_qaCell.qaBtn setBackgroundImage:IMAGE(@"btn_concerned_bg") forState:UIControlStateNormal];
                [_qaCell.qaBtn setImage:[IMAGE(@"btn_ed_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                [_qaCell.qaBtn setTitle:@"已关注" forState:UIControlStateNormal];
            }
            else {
                [_qaCell.qaBtn setBackgroundImage:IMAGE(@"btn_unconcern_bg") forState:UIControlStateNormal];
                [_qaCell.qaBtn setImage:[IMAGE(@"btn_un_concern") imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] forState:UIControlStateNormal];
                [_qaCell.qaBtn setTitle:@"  关注" forState:UIControlStateNormal];
            }

            
            _qaCell.praise.tag = 1000+indexPath.row;
            _qaCell.disPraise.tag = 1000+indexPath.row;
            _qaCell.qaBtn.tag = 1000+indexPath.row;
            
            [_qaCell.qaBtn addTarget:self action:@selector(focusClick:) forControlEvents:UIControlEventTouchUpInside];
            [_qaCell.praise addTarget:self action:@selector(praiseClick:) forControlEvents:UIControlEventTouchUpInside];
            [_qaCell.disPraise addTarget:self action:@selector(disPraiseClick:) forControlEvents:UIControlEventTouchUpInside];
            
        }
        
        if (indexPath.row != 0) {
            _qaCell.backgroundColor = kClearColor;
            
            // 直接用问答主页的，所以cell样式需要修改
            _qaCell.line.hidden = YES;
            _qaCell.qaBtn.hidden = YES;
            _qaCell.viewNum.hidden = YES;
            _qaCell.commentNum.hidden = YES;
            _qaCell.gender.hidden = YES;
            _qaCell.money.hidden = YES;
        }
        
        if ([[GetUserDefaule(USERINFO) valueForKey:@"user_id"] isEqualToString:[model.author valueForKey:@"user_id"]]) {
            _qaCell.deleteBtn.hidden = NO;
            _qaCell.deleteBtn.tag = indexPath.row+10;
            [_qaCell.deleteBtn addTarget:self action:@selector(deleteCommentClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else {
            _qaCell.deleteBtn.hidden = YES;
        }
        
        return _qaCell;
    }
    else {
    
        _cell = [tableView dequeueReusableCellWithIdentifier:@"comment"];
        _cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        if (self.type == 2) {
            ArticleComment *model = self.commentArr[indexPath.row];
            _cell.articleModel = model;
            
            if ([[GetUserDefaule(USERINFO) valueForKey:@"user_id"] isEqualToString:[model.author valueForKey:@"user_id"]]) {
                _cell.deleteBtn.hidden = NO;
                _cell.deleteBtn.tag = indexPath.row+10;
                [_cell.deleteBtn addTarget:self action:@selector(deleteCommentClick:) forControlEvents:UIControlEventTouchUpInside];
            }
            else {
                _cell.deleteBtn.hidden = YES;
            }
            
        }
        else {
            Comment *model = self.commentArr[indexPath.row];
            _cell.model = model;
            
            if ([[GetUserDefaule(USERINFO) valueForKey:@"user_id"] isEqualToString:[model.author valueForKey:@"user_id"]]) {
                _cell.deleteBtn.hidden = NO;
                _cell.deleteBtn.tag = indexPath.row+10;
                [_cell.deleteBtn addTarget:self action:@selector(deleteCommentClick:) forControlEvents:UIControlEventTouchUpInside];
            }
            else {
                _cell.deleteBtn.hidden = YES;
            }
            
        }
        
        _cell.praise.tag = indexPath.row+10;
        [_cell.praise addTarget:self action:@selector(commentPraiseClick:) forControlEvents:UIControlEventTouchUpInside];
        
        if (indexPath.row == 0) {
            _cell.backgroundColor = kWhiteColor;
            [_cell.comment addTarget:self action:@selector(supCommentClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        else {
            _cell.backgroundColor = kClearColor;
            _cell.comment.tag = indexPath.row;
            [_cell.comment addTarget:self action:@selector(subCommentClick:) forControlEvents:UIControlEventTouchUpInside];
        }
        
        
    
        return _cell;
    }
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.commentArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.type == 3) {
        if (indexPath.row == 0) {
//            return [_qaCell cellHeight];
            CGFloat cellH = [_qaCell cellHeight];
            if (cellH == 0) {
                return 10;
            }
            else
                return [_qaCell cellHeight]-10;
        }
        else
            return _qaCell.title.y+_qaCell.title.height+10;
    }
    else
        return [_cell cellHeight];
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSLog(@"点击tableView.cell = %ld", indexPath.row);
}

#pragma mark ==========响应方法==========
- (void)GETAnswerDetail {
    
    NSString *url = [NSString stringWithFormat:@"%@/%ld", APIQuestionsAnswers, self.questionId];
    
    [[NetworkTool sharedTool] requestWithName:@"获取答案详情" URLString:url parameters:nil method:@"GET" callBack:^(id responseObject) {
        self.questionModel = [[Question alloc] init];
        [self.questionModel setValuesForKeysWithDictionary:responseObject[@"data"]];
        
        self.reward = [responseObject[@"data"][@"question"][@"reward"] integerValue];
        self.qaUserId = [responseObject[@"data"][@"question"][@"author"][@"user_id"] integerValue];
        self.questionIsClosed = [responseObject[@"data"][@"question"][@"is_closed"] integerValue];
//        [self.commentArr insertObject:model atIndex:0];
//        [_tableView reloadData];
    }];
    
}
// 点赞评论
- (void)commentPraiseClick:(UIButton *)sender {
    NSString *replyId;
    NSString *url;
    if (self.type == 2) {
        ArticleComment *model = self.commentArr[sender.tag-10];
        replyId = [NSString stringWithFormat:@"%ld", model.aId];
        url = [NSString stringWithFormat:@"%@/%@/praises", APIArticlesComments, replyId];
    }
    else {
        Comment *model = self.commentArr[sender.tag-10];
        replyId = [NSString stringWithFormat:@"%ld", model.reply_id];
        url = [NSString stringWithFormat:@"%@/%@/praises", APIReplies, replyId];
    }
    
    
    [HomeServer praise:sender postId:replyId url:url];
}

// 获取评论
- (void)GETComments {
    
    if (self.type == 3) {
        
        if (!self.questionModel) {
            // 如果还没有就延迟一秒
            [self performSelector:@selector(GETComments) withObject:nil afterDelay:0.5f];
            return;
        }
    }
    
    NSString *url;
    if (self.type == 2) {
        url = [NSString stringWithFormat:@"%@/%ld/comments", APIArticlesComments, self.articleModel.aId];
    }
    else if (self.type == 3) {
        url = [NSString stringWithFormat:@"%@/%ld/comments", APIQuestionsAnswers, self.questionId];
    }
    else {
        url = [NSString stringWithFormat:@"%@/%@/replies", APIReplies, [self.model valueForKey:@"reply_id"]];
    }
    NSDictionary *param = @{@"page":[NSString stringWithFormat:@"%ld", self.page],
//                            @"page":,
                            @"per_page":@"10"};
    
    [[NetworkTool sharedTool] requestWithName:@"获取评论" URLString:url parameters:param method:@"GET" callBack:^(id responseObject) {
        
        NSArray *arr = responseObject[@"data"];
        
        if (self.page == 1) {
            [self.commentArr removeAllObjects];
            if (self.type == 2) {
                [self.commentArr addObject:self.articleModel];
            }
            else if (self.type == 3) {
                [self.commentArr addObject:self.questionModel];
            }
            else {
                [self.commentArr addObject:self.model];
            }
        }
        
        if (arr.count == 0) {
            self.page--;
            if (self.page == 0) {
                self.page = 1;
            }
        }
        
        if (self.type == 2) {
            for (NSDictionary *dic in arr) {
                ArticleComment *model = [[ArticleComment alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_commentArr addObject:model];
            }
        }
        else if (self.type == 3) {
            for (NSDictionary *dic in arr) {
                Question *model = [[Question alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_commentArr addObject:model];
            }
        }
        else {
            for (NSDictionary *dic in arr) {
                Comment *model = [[Comment alloc] init];
                [model setValuesForKeysWithDictionary:dic];
                [_commentArr addObject:model];
            }
        }
        

        dispatch_async(dispatch_get_main_queue(), ^{
            [_tableView.mj_header endRefreshing];
            [_tableView.mj_footer endRefreshing];
            [_tableView reloadData];
        });
    }];
    
}
- (void)postCommentClick:(UIImageView *)img {
    if (GetUserDefaule(USERINFO) == NULL) {
        [self presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    CommentViewController *commentVC = [[CommentViewController alloc] init];
    // ui图。type = 2 视频文章模块是没有下面的那个点击
    if (self.type == 3) {
        commentVC.type = self.type;
        commentVC.postId = [NSString stringWithFormat:@"%ld", self.questionId];
        commentVC.pid = @"";
        
    }
    else {
        commentVC.postId = [self.model valueForKey:@"post_id"];
        commentVC.pid = [self.model valueForKey:@"reply_id"];
    }
//    [self presentViewController:commentVC animated:YES completion:nil];
    [self.navigationController presentSemiViewController:commentVC withOptions:@{
                                                                                 KNSemiModalOptionKeys.pushParentBack    : @(NO),
                                                                                 KNSemiModalOptionKeys.animationDuration : @(0.6),
                                                                                 KNSemiModalOptionKeys.shadowOpacity     : @(0.2),
                                                                                 KNSemiModalOptionKeys.backgroundView : [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]]
                                                                                 }];
}
// 父级评论
- (void)supCommentClick:(UIButton *)btn {
    if (GetUserDefaule(USERINFO) == NULL) {
        [self presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    
    CommentViewController *commentVC = [[CommentViewController alloc] init];
    if (self.type == 2) {
        commentVC.postId = [NSString stringWithFormat:@"%ld", self.articleModel.article_id];
        commentVC.pid = [NSString stringWithFormat:@"%ld", self.articleModel.aId];
        commentVC.type = 2;
    }
    else {
        commentVC.postId = [self.model valueForKey:@"post_id"];
        commentVC.pid = [self.model valueForKey:@"reply_id"];
    }
//    [self presentViewController:commentVC animated:YES completion:nil];
    [self.navigationController presentSemiViewController:commentVC withOptions:@{
                                                                                 KNSemiModalOptionKeys.pushParentBack    : @(NO),
                                                                                 KNSemiModalOptionKeys.animationDuration : @(0.6),
                                                                                 KNSemiModalOptionKeys.shadowOpacity     : @(0.2),
                                                                                 KNSemiModalOptionKeys.backgroundView : [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]]
                                                                                 }];
    
}

- (void)subCommentClick:(UIButton *)btn {
    if (GetUserDefaule(USERINFO) == NULL) {
        [self presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    
    CommentViewController *commentVC = [[CommentViewController alloc] init];
    if (self.type == 2) {
        ArticleComment *model = self.commentArr[btn.tag];
        commentVC.postId = [NSString stringWithFormat:@"%ld", self.articleModel.article_id];
//        commentVC.pid = [_commentArr[btn.tag] valueForKey:@"reply_id"];
        commentVC.pid = [NSString stringWithFormat:@"%ld", model.aId];
        commentVC.atName = [[_commentArr[btn.tag] valueForKey:@"author"] valueForKey:@"nickname"];
        commentVC.type = 2;
    }
    else {
        commentVC.postId = [self.model valueForKey:@"post_id"];
        commentVC.pid = [_commentArr[btn.tag] valueForKey:@"reply_id"];
        commentVC.atName = [[_commentArr[btn.tag] valueForKey:@"author"] valueForKey:@"nickname"];
    }
//    [self presentViewController:commentVC animated:YES completion:nil];
    [self.navigationController presentSemiViewController:commentVC withOptions:@{
                                                                                 KNSemiModalOptionKeys.pushParentBack    : @(NO),
                                                                                 KNSemiModalOptionKeys.animationDuration : @(0.6),
                                                                                 KNSemiModalOptionKeys.shadowOpacity     : @(0.2),
                                                                                 KNSemiModalOptionKeys.backgroundView : [[UIImageView alloc] initWithImage:[UIImage imageNamed:@""]]
                                                                                 }];
}

// 关注
- (void)focusClick:(UIButton *)sender {
    
    Question *model = self.commentArr[sender.tag-1000];
    NSString *toId = [model.author valueForKey:@"user_id"];
    
    [HomeServer focus:sender toId:toId];
}

// 点赞答案
- (void)praiseClick:(UIButton *)sender {
    Question *model = self.commentArr[sender.tag-1000];
    NSString *aId = [NSString stringWithFormat:@"%ld", model.qId];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/likes", APIQuestionsAnswers, aId];
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:sender.tag-1000 inSection:0];
    QAZoneTableViewCell *cell = [_tableView cellForRowAtIndexPath:index];
    
    UIButton *otherBtn = cell.disPraise;
    
    [HomeServer praiseLogicCurrBtn:sender otherBtn:otherBtn url:url];
}
// 踩答案
- (void)disPraiseClick:(UIButton *)sender {
    Question *model = self.commentArr[sender.tag-1000];
    NSString *aId = [NSString stringWithFormat:@"%ld", model.qId];
    
    NSString *url = [NSString stringWithFormat:@"%@/%@/unlikes", APIQuestionsAnswers, aId];
    
    
    NSIndexPath *index = [NSIndexPath indexPathForRow:sender.tag-1000 inSection:0];
    QAZoneTableViewCell *cell  = [_tableView cellForRowAtIndexPath:index];
    
    UIButton *otherBtn = cell.praise;
    
    //    [self praiseLogicCurrBtn:sender otherBtn:otherBtn url:url];
    [HomeServer praiseLogicCurrBtn:sender otherBtn:otherBtn url:url];
}

// 删除评论
- (void)deleteCommentClick:(UIButton *)sender {
    
    NSInteger index = sender.tag-10;
    
    NSString *url;
    if (self.type == 2) {
        ArticleComment *model = self.commentArr[index];
        NSString *replyId = [NSString stringWithFormat:@"%ld", model.aId];
        url = [NSString stringWithFormat:@"%@/%@", APIArticlesComments, replyId];
    }
    else if (self.type == 1) {
        Comment *model = self.commentArr[index];
        NSString *replyId = [NSString stringWithFormat:@"%ld", model.reply_id];
        url = [NSString stringWithFormat:@"%@/%@", APIReplies, replyId];
    }
    else if (self.type == 3) {
        Question *model = self.commentArr[index];
        NSString *replyId = [NSString stringWithFormat:@"%ld", model.qId];
        if (index == 0) {
            url = [NSString stringWithFormat:@"%@/%@", APIQuestionsAnswers, replyId];
        }
        else
            url = [NSString stringWithFormat:@"%@/comments/%@", APIQuestionsAnswers, replyId];
    }
    
    [[NetworkTool sharedTool] requestWithName:@"删除评论" URLString:url parameters:nil method:@"DELETE" callBack:^(id responseObject) {
        
        if (index == 0) {
            [self.navigationController popViewControllerAnimated:YES];
        }
        else {
            [self.commentArr removeObjectAtIndex:sender.tag-10];
            [_tableView reloadData];
        }
    }];
    
}

- (void)payQuestionClick:(UIButton *)sender {
    
    if (GetUserDefaule(USERINFO) == NULL) {
        [self presentViewController:[LZCTools alertMessage:@"请先登录"]animated:YES completion:nil];
        return;
    }
    
    self.cainaBtn = sender;
    
    NSLog(@"点击确认付款");
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"请选择支付方式" message:nil preferredStyle:UIAlertControllerStyleAlert];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"微信支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSDictionary *param = @{@"channel":@"wx_app"};
        
        NSString *url = [NSString stringWithFormat:@"%@/%ld/adoptions", APIQuestionsAnswers, self.questionId];
        
        [[NetworkTool sharedTool] requestWithName:@"确认付款" URLString:url parameters:param method:@"POST" callBack:^(id responseObject) {
            
            NSDictionary *orderString = responseObject[@"data"][@"param_obj"];
            [AliPayServer wxPay:orderString];
            
        }];
    }]];
    
    [alert addAction:[UIAlertAction actionWithTitle:@"支付宝支付" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        NSDictionary *param = @{@"channel":@"ali_app"};
        
        NSString *url = [NSString stringWithFormat:@"%@/%ld/adoptions", APIQuestionsAnswers, self.questionId];
        
        [[NetworkTool sharedTool] requestWithName:@"确认付款" URLString:url parameters:param method:@"POST" callBack:^(id responseObject) {
            
            //        [self returnAction:nil];
            //        [self.navigationController popViewControllerAnimated:YES];
            
            NSString *orderString = responseObject[@"data"][@"param_str"];
            [AliPayServer aliPay:orderString memo:@"采纳成功"];
            
        }];
    }]];
    
    [self presentViewController:alert animated:YES completion:nil];
    
    
    
}

#pragma mark ==========代理方法==========
//- (void)textFieldDidBeginEditing:(UITextField *)textField {
//    CommentViewController *commentVC = [[CommentViewController alloc] init];
//    commentVC.postId = [self.model valueForKey:@"post_id"];
//    commentVC.pid = [self.model valueForKey:@"reply_id"];
//    [self presentViewController:commentVC animated:YES completion:nil];
//}

// 懒加载
- (NSMutableArray *)commentArr {
    if (_commentArr == nil) {
        if (self.type == 2) {
            _commentArr = [[NSMutableArray alloc] initWithObjects:self.articleModel, nil];
        }
        else
            _commentArr = [[NSMutableArray alloc] initWithObjects:self.model, nil];
    }
    return _commentArr;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
